package com.yangit.temp.common.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Test;

public class Snippet {
	@Test
	public void test3() throws Exception {
		//System.getProperties().setProperty("proxySet", "true");
		//System.getProperties().setProperty("http.proxyHost", "60.191.201.38"); // 设置ip
		//System.getProperties().setProperty("http.proxyPort", "45461"); // 设置端口号

		//Document cocument = Jsoup.connect("http://127.0.0.1:8082/newAtb/indexData.do").ignoreContentType(true).post();
		setProxyIpList();
	}

	// 至于代理ip，有很多种获取方式，大部分需要花钱买，免费的很少，这里给大家一个网站
	// http://www.xicidaili.com/，这个网站每天都会更新免费的代理ip，我们可以定期去上面爬取数据到本地供我们使用，这里贴上一段本人爬取ip的代码
	public void setProxyIpList() {
		try {
			String rLine;
			URL url = new URL("http://www.xicidaili.com/");
			URLConnection conn = url.openConnection();
			conn.setRequestProperty("User-Agent",
					"Mozilla/4.0 (compatible; MSIE 7.0; NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");
			List<String> list = new ArrayList<>();
			InputStreamReader inputStreamReader = new InputStreamReader(conn.getInputStream(), "utf-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			int a = 0;
			do {
				rLine = bufferedReader.readLine();
				String reg1 = "\\d+"; // 匹配端口号
				String reg = "\\d+\\.\\d+\\.\\d+\\.\\d+"; // 匹配ip
				if (rLine.contains("<td>")) {
					rLine = rLine.substring(rLine.indexOf("<td>") + 4, rLine.indexOf("</td>"));
					if (Pattern.matches(reg, rLine) || Pattern.matches(reg1, rLine)) {
						list.add(rLine);
						a++;
					}
				}
			} while (a != 40);

			for (int i = 0; i < list.size(); i = i + 2) {
				System.out.println("ip:"+list.get(i)+",port:"+list.get(i + 1));
//				ip.setIp(list.get(i));
//				ip.setPort(Integer.parseInt(list.get(i + 1)));

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
